/* 
首页功能模块相关数据管理
*/

import {
  RECEIEVE_SHOPS,
  RECEIEVE_ADDRESS,
  RECEIEVE_ADDRESSING,
  RECEIEVE_CATEGORYS,
}
from '../mutations._type.js'
import {
  reqAddress,
  reqFoodCategorys,
  reqShops,
} from '../../api'
export default {
  state: {
    address: '',
    categorys: [],
    shops: [],
  },
  mutations: {
    // 地址请求中
    [RECEIEVE_ADDRESSING](state) {
      const address = {
        address: '定位中...'
      }
      state.address = address
    },
    [RECEIEVE_ADDRESS](state, address) {
      state.address = address
    },
    [RECEIEVE_CATEGORYS](state, categorys) {
      state.categorys = categorys
    },
    [RECEIEVE_SHOPS](state, shops) {
      state.shops = shops
    },
  },
  actions: {
    /* 
      获取当前地址信息对象的异步action
      */
    async getAddress({
      commit
    }) {
      // 在发请求中
      commit(RECEIEVE_ADDRESSING)
      // 发异步请求
      const result = await reqAddress(32.73763, 115.04696)
      if (result.code === 0) {
        const address = result.data
        // 将状态提交到mutations里，在mutations中更新状态数据
        setTimeout(() => {
          commit(RECEIEVE_ADDRESS, address)
        }, 2000);
      }

    },


    /* 
    获取当前食品商品分类的异步action
    */
    async getFoodCategorys({
      commit
    }, callback) {
      // 发异步请求
      const result = await reqFoodCategorys()
      if (result.code === 0) {
        const categorys = result.data
        // 将状态提交到mutations里，在mutations中更新状态数据
        commit(RECEIEVE_CATEGORYS, categorys)
        //commit mutation方法后数据已经更新了 就可以 调用callback new swiper
        typeof callback === 'function' && callback() //判断callback必须是函数类型才能调用
      }

    },

    /* 
    获取根据经纬度获取商铺列表的异步action
    */
    async getShops({
      commit
    }) {
      // 发异步请求
      const result = await reqShops(38.957975, 113.665412)
      if (result.code === 0) {
        const shops = result.data

        // 将状态提交到mutations里，在mutations中更新状态数据
        commit(RECEIEVE_SHOPS, shops)
      }

    },
  },
  getters: {

  }
}